package com.afkt.project;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.security.SystemPermission;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import static ohos.bundle.IBundleManager.PERMISSION_GRANTED;

public class MainAbility extends Ability {

    private static final int PERMISSION_REQUEST_CODE = 1;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        requestCameraPermission(getContext());
    }

    private void requestCameraPermission(Context context) {
        List<String> permissions = new LinkedList<String>(Arrays.asList(
                SystemPermission.WRITE_USER_STORAGE,
                SystemPermission.READ_USER_STORAGE,
                SystemPermission.CAMERA,
                SystemPermission.READ_MEDIA,
                SystemPermission.WRITE_MEDIA,
                SystemPermission.MEDIA_LOCATION));
        permissions.removeIf(
                permission -> context.verifySelfPermission(permission) == PERMISSION_GRANTED || !context.canRequestPermission(permission));
        if (!permissions.isEmpty()) {
            context.requestPermissionsFromUser(permissions.toArray(new String[permissions.size()]), PERMISSION_REQUEST_CODE);
        }
    }

    /**
     * 申请权限返回结果
     *
     * @param requestCode  请求码
     * @param permissions  权限集合
     * @param grantResults 返回结果集
     */
    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode != PERMISSION_REQUEST_CODE) {
            return;
        }

    }
}
